import Vue from 'vue'
import Vuex from 'vuex'
import {getToken,setToken,removeToken} from "@/utils/auth.js"
import api from "../api/index.js"
import md5 from "js-md5";

Vue.use(Vuex)

const getDefaultState = () => {
  return {
	  token:getToken(),
	  info:'',
	  currentSchool:'',
	  windowHeight:''
  }
}

const state = getDefaultState()

const mutations = {
	RESET_STATE: (state) => {
	  Object.assign(state, getDefaultState())
	},
	SET_TOKEN: (state, token) => {
	  state.token = token
	},
	SET_INFO: (state, info) => {
	  state.info = info
	},
	SET_SCHOOL: (state, school) => {
	  state.currentSchool = school
	},
	SET_HEIGHT: (state, windowHeight) => {
	  state.windowHeight = windowHeight
	},
}

const actions = {
	loginByToken({commit}){
		return new Promise((resolve, reject) => {
			api.loginByToken().then(response =>{
				const { data } = response.data
				commit('SET_TOKEN', data.token)
				commit('SET_INFO', JSON.stringify(data))
				setToken(data.token)
				resolve(data)
			}).catch(error => {
				console.log(error)
				// uni.hideLoading()
				reject(error)
			})
		})
	},
	logintap({commit},userinfo){
		// uni.showLoading({
		// 	title:"loading..."
		// })
		return new Promise((resolve, reject) => {
			// let response = {"status":200,"data":{"id":180,"username":"wangfeng","password":"8de7785f54fe9fdf18d90b69c2c595c2","name":"\u738b\u950b","id_card":"","phone":"18221406061","note":"","qq":"","address":"","status":"\u6b63\u5e38","operator_id":164,"look_level":1,"created_at":"2019-02-21 09:33:45","updated_at":"2019-07-09 11:28:43","belong_shop":0,"group_title":"\u8d85\u7ea7\u7ba1\u7406\u5458","mini_rule_name":null,"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTgwLCJ1c2VybmFtZSI6IndhbmdmZW5nIiwicGFzc3dvcmQiOiI4ZGU3Nzg1ZjU0ZmU5ZmRmMThkOTBiNjljMmM1OTVjMiIsIm5hbWUiOiJcdTczOGJcdTk1MGIiLCJpZF9jYXJkIjoiIiwicGhvbmUiOiIxODIyMTQwNjA2MSIsIm5vdGUiOiIiLCJxcSI6IiIsImFkZHJlc3MiOiIiLCJzdGF0dXMiOiJcdTZiNjNcdTVlMzgiLCJvcGVyYXRvcl9pZCI6MTY0LCJsb29rX2xldmVsIjoxLCJjcmVhdGVkX2F0IjoiMjAxOS0wMi0yMSAwOTozMzo0NSIsInVwZGF0ZWRfYXQiOiIyMDE5LTA3LTA5IDExOjI4OjQzIiwiYmVsb25nX3Nob3AiOjAsImdyb3VwX3RpdGxlIjoiXHU4ZDg1XHU3ZWE3XHU3YmExXHU3NDA2XHU1NDU4IiwibWluaV9ydWxlX25hbWUiOm51bGwsInRpbWVzdGFtcCI6MTU5Njc5MjAwN30.McACNgpSUXaax13d8--2fk6ZXLG0KeLaYhKebPPzwuM"},"msg":"\u67e5\u8be2\u6210\u529f"};
			
			// 这里不能用解构是因为login是在api下面的属性，而不是一个集合
			api.login({username:userinfo.username,password:md5(userinfo.password)}).then(response =>{
				const { data } = response.data
				commit('SET_TOKEN', data.token)
				commit('SET_INFO', JSON.stringify(data))
				setToken(data.token)
				resolve(data)
			}).catch(error => {
				console.log(error)
				// uni.hideLoading()
				reject(error)
			})
		})
	},
	
	
	// remove token
	resetToken({ commit }) {
	  return new Promise(resolve => {
	    removeToken() // must remove  token  first
	    commit('RESET_STATE')
	    resolve()
	  })
	}
}

const getters = {
	token :state => {
		console.log(state.token)
		return state.token
	},
	currentSchool :state => JSON.parse(state.currentSchool),
	windowHeight: state => state.windowHeight,
	info :state => {
		return JSON.parse(state.info)	
	}
}

const store = new Vuex.Store({
	state,
	mutations,
	actions,
	getters
})


export default store